package com.hanagent.controller;

import com.alibaba.fastjson.JSONObject;
import com.hanagent.model.Admins;
import com.hanagent.model.Agents;
import com.hanagent.model.Tokens;
import com.hanagent.utils.ConstantUtils;
import com.hanagent.utils.DateUtils;
import com.hanagent.utils.ResponseUtils;
import com.hanagent.utils.UUidUtils;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

public class AdminController extends Controller{
	
	public void getList() {
		
		Agents loginAgents = getAttr("loginAgents"); //登陆的代理商
		
		int pageNumber = getParaToInt("pageNumber")<1?1:getParaToInt("pageNumber");
		int pageSize = getParaToInt("pageSize")<1?1:getParaToInt("pageSize");
		Page<Admins> list = Admins.dao.paginate(pageNumber, pageSize, "select *", "from Admins where agentId = ? order by updatedAt desc",
				loginAgents.getAgentId());
        renderText(ResponseUtils.sendResult(1, list, "获取数据成功"));
    }
	
	/**  
	 * 管理员登陆
     */  
    public void adminLogin() {  
    	String phone = getPara("phone");
    	String psw = getPara("psw");
    	Admins admins = Admins.dao.findFirst("select * from Admins where adminPhone=\"" + phone + "\" and adminPassword=\"" + psw + "\"");
        if (admins!=null) {  
        	String uuid = UUidUtils.getUUid();
        	new Tokens().set("token", uuid).set("tokenType", "2").set("tokenUserId", admins.getAdminId()).save();
        	admins.setToken(uuid);
        	Agents agents = Agents.dao.findById(admins.getAgentId());
        	JSONObject object = new JSONObject();
        	object.put("admin", admins);
        	object.put("agent", agents);
        	renderText(ResponseUtils.sendResult(1, object, "登陆成功"));  
        }else {  
        	renderText(ResponseUtils.sendResult(0, "", "登陆失败，用户名或密码错误"));  
        }  
    }
	
	/**  
	 * 添加管理员
     */  
    public void addAdmin() {  
    	Admins admins = getModel(Admins.class, "");//别名  Jfinal 必杀技快速开发全靠它  
    	Agents loginAgents = getAttr("loginAgents"); //登陆的代理商
    	admins.setAgentId(loginAgents.getAgentId());
        boolean flag = admins.save();  
        if (flag) {  
        	renderText(ResponseUtils.sendResult(1, "", "添加成功"));  
        }else {  
        	renderText(ResponseUtils.sendResult(0, "", "添加失败"));  
        }  
    }

    /** 
     * 修改 
     */   
    public void updateAdmin() {  
    	Admins admins = getModel(Admins.class, "");//别名  Jfinal 必杀技快速开发全靠它  
    	
        boolean flag = admins.update();  
        if (flag) {  
        	renderText(ResponseUtils.sendResult(1, "", "修改成功"));  
        }else {  
        	renderText(ResponseUtils.sendResult(0, "", "修改失败"));  
        }   
    }  
    
    /** 
     * 修改 
     */   
    public void changePasswd() {  
    	int type = getParaToInt("type");
    	Agents agents = getAttr("loginAgents");
    	boolean flag;
    	if (type==1) {
    		flag = Admins.dao.findById(getPara("adminId")).set("adminPassword", getPara("newPasswd")).set("updatedAt", DateUtils.getNowDateWith8()).update();
		}else{
			flag = agents.set("secPassword", getPara("newPasswd")).set("updateAt", DateUtils.getNowDateWith8()).update();
		}
        if (flag) {  
        	renderText(ResponseUtils.sendResult(1, "", "修改成功"));  
        }else {  
        	renderText(ResponseUtils.sendResult(0, "", "修改失败"));  
        }   
    }
      
    /** 
     * 删除单个 
     */  
    public void delAdminById() {
    	Admins loginAdmins = getAttr("loginAdmins"); //登陆的代理商
    	if (loginAdmins.getType()!=ConstantUtils.TypeAdminSuper) {
    		renderText(ResponseUtils.sendResult(0, "", "只有全权管理员才可以删除管理员"));
    		return;
		}
    	Integer id = getParaToInt("adminId");  
    	Admins delAdmins = Admins.dao.findById(id);
    	int delId = delAdmins.getAdminId();
    	int logId = loginAdmins.getAdminId();
    	if (delId == logId) {
    		renderText(ResponseUtils.sendResult(0, "", "自己不能删除自己"));
    		return;
		}
        boolean flag = Admins.dao.deleteById(id);  
        if (flag) {  
        	renderText(ResponseUtils.sendResult(1, "", "删除成功"));  
        }else {  
        	renderText(ResponseUtils.sendResult(0, "", "删除失败"));  
        }    
    }  
	

}
